﻿using CIS.Web.Common;
using CIS.Web.Models;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[AllowAnonymous]
	[RoutePrefix("MeetingMain")]
	public class MeetingMainController : BaseDbController
	{
		[Route("Index/{name}")]
		public async Task<ActionResult> Index(string name)
		{
			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ConferenceNickName == name && x.Status == ConstValues.Status.正常);

			if (conferenceMaster == null)
			{
				return RedirectToAction(nameof(HomeController.MainIndex), nameof(HomeController).GetControllerName());
			}

			var sql = from newListInfo in DbContext.NewsListInfo.AsNoTracking()
					.Where(x => x.ConferenceId == conferenceMaster.ID &&
						x.Status == ConstValues.Status.正常 &&
						x.NewsListName == ConstValues.Slider)
				  join newInfo in DbContext.NewsInfo.AsNoTracking().Where(x => x.Status == ConstValues.Status.正常)
				  on newListInfo.ID equals newInfo.NewsListId
				  orderby newInfo.Sort ascending, newInfo.UpdateDate descending
				  select new
				  {
					  ID = newInfo.ID,
					  Sort = newInfo.Sort,
					  Link = newInfo.Link,
					  Title = newInfo.NewsTitle
				  };

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new MeetingSliderModel
			{
				Id = x.ID,
				Link = x.Link,
				Title = x.Title
			});

			var model = new ConferenceMasterModel();
			model.MeetingSliderList = result.ToList();
			model.Id = conferenceMaster.ID;
			model.ConferenceName = conferenceMaster.ConferenceName;
			model.Introduction = conferenceMaster.Introduction == null ? string.Empty : conferenceMaster.Introduction;
			model.ConferenceModel = conferenceMaster.ConferenceModel;
			model.IsInternational = conferenceMaster.IsInternational;
			model.Keywords = conferenceMaster.Keywords;
			model.Description = conferenceMaster.Description;

			model.IsShowCountdown = conferenceMaster.IsShowCountdown;
			model.PicForBackgroundPath = string.IsNullOrEmpty(conferenceMaster.PicForBackground) ? string.Empty :
						conferenceMaster.PicForBackground.Replace("~", "").Replace("\\", "/");
			model.OpacityOfPic = conferenceMaster.OpacityOfPic;
			model.IsFollowBackground = conferenceMaster.IsFollowBackground;
			model.ColorForBackground = conferenceMaster.ColorForBackground;
			model.OpacityOfColor = conferenceMaster.OpacityOfColor;
			model.ColorForMenu = conferenceMaster.ColorForMenu;
			model.OpacityOfMenu = conferenceMaster.OpacityOfMenu;
			model.ColorForLeft = conferenceMaster.ColorForLeft;
			model.OpacityOfLeft = conferenceMaster.OpacityOfLeft;
			model.ColorForCenter = conferenceMaster.ColorForCenter;
			model.OpacityOfCenter = conferenceMaster.OpacityOfCenter;
			model.ColorForRight = conferenceMaster.ColorForRight;
			model.OpacityOfRight = conferenceMaster.OpacityOfRight;
			model.ColorForConference = conferenceMaster.ColorForConference;
			model.FontColorForMenu = conferenceMaster.FontColorForMenu;

			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", conferenceMaster.ID);

			ViewBag.ConferenceId = conferenceMaster.ID;
			ViewBag.ConferenceName = conferenceMaster.ConferenceName;

			return View(model);
		}

		//消息分类 -- 中
		public async Task<JsonCamelCaseResult> GetCenterNewsList(string id)
		{
			var sql = DbContext.NewsListInfo.AsNoTracking()
				.Where(x => x.Status == ConstValues.Status.正常 &&
					x.ConferenceId == id &&
					x.NewsListPosition == ConstValues.ConferenceModelStyle.Center &&
					x.NewsListName != ConstValues.Banner &&
					x.NewsListName != ConstValues.Slider)
				.Join(DbContext.NewsInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常)
					.GroupBy(x => new { x.NewsListId }).Select(x => new { NewsListId = x.Key.NewsListId }),
					newsList => newsList.ID,
					news => news.NewsListId,
					(newsList, news) => new
					{
						Id = newsList.ID,
						Link = newsList.Link,
						NewsListName = newsList.NewsListName,
						NewsListPic = newsList.NewsListPic,
						NewsStyle = newsList.NewsStyle,
						BackgroundColor = newsList.BackgroundColor,
						BackgroundOpacity = newsList.BackgroundOpacity,
						FontSize = newsList.FontSize,
						FontWeight = newsList.FontWeight,
						FontStyle = newsList.FontStyle,
						FontColor = newsList.FontColor,
						LineColor = newsList.LineColor,
						IsShowMore = newsList.IsShowMore,
						MoreColor = newsList.MoreColor,
						Sort = newsList.Sort,
						CreateDate = newsList.CreateDate
					});

			sql = sql.OrderBy(x => x.Sort).ThenByDescending(x => x.CreateDate);

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.Id,
				Link = x.Link,
				NewsListName = x.NewsListName,
				NewsListPic = x.NewsListPic,
				NewsStyle = x.NewsStyle,
				BackgroundColor = x.BackgroundColor,
				BackgroundOpacity = x.BackgroundOpacity,
				FontSize = x.FontSize,
				FontWeight = x.FontWeight,
				FontStyle = x.FontStyle,
				FontColor = x.FontColor,
				LineColor = x.LineColor,
				IsShowMore = x.IsShowMore,
				MoreColor = x.MoreColor,
			});

			return JsonCamelCase(result, JsonRequestBehavior.AllowGet);
		}

		//消息分类 -- 右
		public async Task<JsonCamelCaseResult> GetRightNewsList(string id)
		{
			var sql = DbContext.NewsListInfo.AsNoTracking()
				.Where(x => x.Status == ConstValues.Status.正常 &&
					x.ConferenceId == id &&
					x.NewsListPosition == ConstValues.ConferenceModelStyle.Right &&
					x.NewsListName != ConstValues.Banner &&
					x.NewsListName != ConstValues.Slider)
				.Join(DbContext.NewsInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常)
					.GroupBy(x => new { x.NewsListId }).Select(x => new { NewsListId = x.Key.NewsListId }),
					newsList => newsList.ID,
					news => news.NewsListId,
					(newsList, news) => new
					{
						Id = newsList.ID,
						Link = newsList.Link,
						NewsListName = newsList.NewsListName,
						NewsListPic = newsList.NewsListPic,
						NewsStyle = newsList.NewsStyle,
						BackgroundColor = newsList.BackgroundColor,
						BackgroundOpacity = newsList.BackgroundOpacity,
						FontSize = newsList.FontSize,
						FontWeight = newsList.FontWeight,
						FontStyle = newsList.FontStyle,
						FontColor = newsList.FontColor,
						LineColor = newsList.LineColor,
						IsShowMore = newsList.IsShowMore,
						MoreColor = newsList.MoreColor,
						Sort = newsList.Sort,
						CreateDate = newsList.CreateDate
					});

			sql = sql.OrderBy(x => x.Sort).ThenByDescending(x => x.CreateDate);

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.Id,
				Link = x.Link,
				NewsListName = x.NewsListName,
				NewsListPic = x.NewsListPic,
				NewsStyle = x.NewsStyle,
				BackgroundColor = x.BackgroundColor,
				BackgroundOpacity = x.BackgroundOpacity,
				FontSize = x.FontSize,
				FontWeight = x.FontWeight,
				FontStyle = x.FontStyle,
				FontColor = x.FontColor,
				LineColor = x.LineColor,
				IsShowMore = x.IsShowMore,
				MoreColor = x.MoreColor,
			});

			return JsonCamelCase(result, JsonRequestBehavior.AllowGet);
		}

		//消息分类 -- 左
		public async Task<JsonCamelCaseResult> GetLeftNewsList(string id)
		{
			var sql = DbContext.NewsListInfo.AsNoTracking()
				.Where(x => x.Status == ConstValues.Status.正常 &&
					x.ConferenceId == id &&
					x.NewsListPosition == ConstValues.ConferenceModelStyle.Left &&
					x.NewsListName != ConstValues.Banner &&
					x.NewsListName != ConstValues.Slider)
				.Join(DbContext.NewsInfo.AsNoTracking()
					.Where(x => x.Status == ConstValues.Status.正常)
					.GroupBy(x => new { x.NewsListId }).Select(x => new { NewsListId = x.Key.NewsListId }),
					newsList => newsList.ID,
					news => news.NewsListId,
					(newsList, news) => new
					{
						Id = newsList.ID,
						Link = newsList.Link,
						NewsListName = newsList.NewsListName,
						NewsListPic = newsList.NewsListPic,
						NewsStyle = newsList.NewsStyle,
						BackgroundColor = newsList.BackgroundColor,
						BackgroundOpacity = newsList.BackgroundOpacity,
						FontSize = newsList.FontSize,
						FontWeight = newsList.FontWeight,
						FontStyle = newsList.FontStyle,
						FontColor = newsList.FontColor,
						LineColor = newsList.LineColor,
						IsShowMore = newsList.IsShowMore,
						MoreColor = newsList.MoreColor,
						Sort = newsList.Sort,
						CreateDate = newsList.CreateDate
					});

			sql = sql.OrderBy(x => x.Sort).ThenByDescending(x => x.CreateDate);

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.Id,
				Link = x.Link,
				NewsListName = x.NewsListName,
				NewsListPic = x.NewsListPic,
				NewsStyle = x.NewsStyle,
				BackgroundColor = x.BackgroundColor,
				BackgroundOpacity = x.BackgroundOpacity,
				FontSize = x.FontSize,
				FontWeight = x.FontWeight,
				FontStyle = x.FontStyle,
				FontColor = x.FontColor,
				LineColor = x.LineColor,
				IsShowMore = x.IsShowMore,
				MoreColor = x.MoreColor,
			});

			return JsonCamelCase(result, JsonRequestBehavior.AllowGet);
		}
	}
}